/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package others;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Dingo
 */
@ManagedBean(name="login")
@SessionScoped
public class Login implements Serializable {

    private String login = null;
    private String pass;
    private boolean loggedIn = false;
    private User user;

    /** Creates a new instance of Login */
    public Login() {
        user = new User(null, null, User.PERM_UNLOGGED);
    }
    
    /**
     * Metoda sprawdza czy zalogowany użytkownik ma dostęp do metody
     * @param perm -> minimalne uprawnienia jakie powinien mieć użytkownik, aby się dostać do metody
     * @return zwraca true, jeżeli można, false jeżeli nie można
     */
    public boolean userPermited(int perm){
        if(user.getPermition() >= perm){
            return true;
        }
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "BŁĄD!",
                    "Posiadasz niewystarczające uprawienia, aby wykonać tę metodę!");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return false;
    }

    public void doLogin(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (login != null && login.equals("admin") && pass != null && pass.equals("admin")) {
            loggedIn = true;
            user = new User(login, pass, User.PERM_SUPER_ADMIN);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Witaj", login);
            login = null;
            pass = null;
        } else {
            loggedIn = false;
            login = null;
            user = new User(null, null, User.PERM_UNLOGGED);
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Błąd logowania", "Podano nieprawidłowe dane.");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
    }

    public void doLogout(ActionEvent actionEvent) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Wylogowano", login);
        login = null;
        pass = null;
        loggedIn = false;
        user = new User(null, null, User.PERM_UNLOGGED);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
